package com.loop.unclelend.ui.fragment

import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
import android.graphics.Color
import android.view.LayoutInflater
import android.view.View
import com.loop.unclelend.R
import com.loop.unclelend.databinding.UiPagePinPaymentFailedBinding
import com.loop.unclelend.pro.ProFragment
import com.loop.unclelend.ui.activity.Act_Pin_Payment_Status
import com.loop.unclelend.utils.event.UncleEventHelper
import com.loop.unclelend.utils.event.UncleEventType
import org.json.JSONObject

/**
 * UncleLend
 * @Author wwdeng
 * @CreateTime: 2024/9/28 17:04
 * @Annotation：支付失败
 */
class Fm_Pin_Payment_Failed(): ProFragment(){
    lateinit var binding: UiPagePinPaymentFailedBinding
    private var orderData: JSONObject? = null
    private var act: Act_Pin_Payment_Status? = null

    constructor(act: Act_Pin_Payment_Status, data: JSONObject): this(){
        this.act = act
        orderData = data
    }

    override fun getUncleRootView(inflater: LayoutInflater): View {
        binding = UiPagePinPaymentFailedBinding.inflate(inflater)
        return binding.root
    }

    override fun initUncleView() {
        setUncleTitleBarVisibility(View.VISIBLE)
        setUncleBackBtnRes(R.mipmap.icon_back)
        setUncleTextTitle(getString(R.string.loan_payment_status_title))
        setUncleRightBtnRes(R.mipmap.icon_kefu)
    }

    override fun initUncleListener() {
        //客服
        rootViewBinding.imgKefu.setOnClickListener {
            showUncleKefuDialog("PIN PaymentFailed Page")
            UncleEventHelper.addUncleRequestEventAction(UncleEventType.CLICK.UNCLE_CLICK_CUSTOMER,"PIN PaymentFailed Page")
        }
        binding.imgCopy.setOnClickListener {
            onUncleCopyAction(orderData!!.optString("transactionId"))
        }

        binding.btnPaymentAgain.setOnClickListener {
            UncleEventHelper.addUncleRequestEventAction(UncleEventType.CLICK.UNCLE_CLICK_LOAN_PAYMENT_FAILED_PAYMENT_AGAIN,"TransactionId：${orderData!!.optString("transactionId")}")
            act!!.presenter!!.requestUncleLoanPayLinkAction()
        }
    }

    override fun initUncleData() {
        refreshData(orderData!!)
    }

    /**
     * 刷新数据
     */
    fun refreshData(data: JSONObject){
        orderData = data
        binding.txtAmount.text = data.optString("realRepayAmount","")
        binding.txtProduct.text = data.optString("product","")
        binding.txtThirdOrderId.text = data.optString("transactionId","")
        binding.txtFailedTime.text = data.optString("failedTime","")
    }

    /**
     * 复制
     */
    private fun onUncleCopyAction(orderId: String){
        UncleEventHelper.addUncleRequestEventAction(UncleEventType.CLICK.UNCLE_CLICK_LOAN_PAYMENT_FAILED_COPY, "TransactionId：$orderId")
        // 获取剪贴板管理器
        val clipboardManager = act!!.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
        // 创建一个 ClipData 对象，将要复制的文本放入其中
        val clipData = ClipData.newPlainText("label", orderId)
        // 将 ClipData 对象放入剪贴板
        clipboardManager.setPrimaryClip(clipData)
        showUncleToast(getString(R.string.dialog_kefu_copy_success))
    }

    override fun onStart() {
        super.onStart()
        UncleEventHelper.addUncleRequestEventAction(UncleEventType.PAGE.UNCLE_PAGE_LOAN_PIN_REPAYMENT_FAILED_START)
    }

    override fun onDestroy() {
        super.onDestroy()
        UncleEventHelper.addUncleRequestEventAction(UncleEventType.PAGE.UNCLE_PAGE_LOAN_PIN_REPAYMENT_FAILED_END)
    }
}